﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Medianamik.Core.Configuration;
using System.Web.Hosting;
using System.Reflection;
using Medianamik.Core.Resources;

namespace TypeManagement.Controls
{
    public partial class NodeTypeIcons : System.Web.UI.Page
    {
        protected static MedianamikConfigSection Config =(MedianamikConfigSection)ConfigurationManager.GetSection(Medianamik.Core.ConfigurationSectionName.MEDIANAMIK);

        private static readonly string[] ImageExtensions = new string[] { ".png", ".gif", ".jpg", ".jpeg" };
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack )
            {
                rptIcons.DataBind();
            }
        }

        protected  List<string> GetFiles()
        {
            List<string> images = new List<string>();
            foreach (var pathElm in Config.NodeTypeIcons.Cast<PathElement>())
            {
                AddFilesRecursive(pathElm.Path , images);
            }
            //Ajouter la liste des images embedded dans Medianamik
            AddEmbeddedFiles(images);

            //Eviter les doublons  avec les images embedded
            images = images.Distinct(new IconEqualityComparer()).ToList();
            
            images.Sort();
            
            return images;
        }

        private void AddEmbeddedFiles(List<string> images)
        {
            var assembly = ResourcesUtil.GetWebAssembly();
            var infos = assembly.GetResourceInfos()
                .Where(ri => ri.ResourceName.StartsWith("images.NodeTypeIcons", StringComparison.InvariantCultureIgnoreCase));

            foreach (var resourceInfo in infos)
            {
                images.Add(resourceInfo.VirtualPath.TrimStart('~'));
            }
        }

        private void AddFilesRecursive(string pPath, List<String> pFiles)
        {
            DirectoryInfo dir = new DirectoryInfo(Page.Server.MapPath(pPath));
            if (dir.Exists)
            {
                pFiles.AddRange(dir.GetFiles("*.*", SearchOption.TopDirectoryOnly)
                    .Where(f => ImageExtensions.Any(e => e.Equals(f.Extension, StringComparison.OrdinalIgnoreCase)))
                    .Select(f => VirtualPathUtility.AppendTrailingSlash(pPath) + f.Name));
                foreach (var subDir in dir.GetDirectories())
                {
                    AddFilesRecursive(VirtualPathUtility.AppendTrailingSlash(pPath) + subDir.Name, pFiles);
                }
            }
        }

        private class IconEqualityComparer : IEqualityComparer<string>
        {
            #region IEqualityComparer<string> Members

            public bool Equals(string x, string y)
            {
                return GetHashCode(x).CompareTo(GetHashCode(y)) == 0;
            }

            public int GetHashCode(string obj)
            {
                return obj.ToLowerInvariant().GetHashCode(); 
            }

            #endregion
        }

    }
}
